using System.Web.Services;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-03-08	
	Status:		release	
	Version:	2.0.0
	Build:		20071030
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu EngineX.BlogX 
	/// This module is a component of the EngineX blogging and content management engine
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class recruitX : x_result {
		#region Invisible properties
		x_user userx;
		x_recruit recruitx;
		x_blogspace blogspacex;

		private const string DEFAULT_GROUP = "public";
		private const string DEFAULT_SECRET = "umlungu";
		private const string DEFAULT_PASSWORD = "password";
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public recruitX() : base("blogX") {
			userx = new x_user(Application);
			recruitx = new x_recruit(Application);
			blogspacex = new x_blogspace(Application);
		}
		#endregion

		#region General web service methods
		/// <summary>Logs a recruit on to the system</summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		/// <param name="username">login username</param>
		/// <param name="password">login password</param>
		[WebMethod] public XmlDocument Token(string username, string password) {
			try {
				userx.Login(username, password);
				_AddOk("token", userx.Token);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Register a new recruit on the system</summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument Validate(string token) {
			try {
				userx.Validate(token);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/*
		/// <summary>Tests</summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument Test(string token) {
			try {
				_AddOk(userx.Users);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		*/
		#endregion

		#region Recruit manage web service methods
		/// <summary>Adds a new recruit to the system</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the recruit</param>
		/// <param name="firstname">the recruit's first name</param>
		/// <param name="surname">the recruit's surname</param>
		/// <param name="email">the recruit's emaill address</param>
		[WebMethod]
		public XmlDocument Add(string token, string name, string firstname, string surname, string email) {
			try {
				userx.Validate(token, "Recruit.Add");
				recruitx.Add(name, email, firstname, surname);
				recruitx.SendConfirmation(name);
				_AddOk(recruitx.Recruit.OuterXml);
			} catch (x_exception e) { _AddError(e); } catch (System.Exception e) { _AddError(e); }
			return (Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="guid">the unique identifier of the recruit</param>
		[WebMethod] public XmlDocument Get(string token, string uid) {
			try {
				userx.Validate(token, "Recruit.Get");
				recruitx.Get(uid);
				_AddOk(recruitx.Recruit.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/* not sure if these are required...
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument List(string token) {
			try {
				userx.Validate(token, "Recruit.List");
				_AddOk(recruitx.List().OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the recruit</param>
		[WebMethod] public XmlDocument Delete(string token, string name) {
			try {
				userx.Validate(token, "Recruit.Delete");
				recruitx.Delete(name);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the recruit</param>
		/// <param name="firstname">the recruit's first name</param>
		/// <param name="surname">the recruit's surname</param>
		/// <param name="email">the recruit's emaill address</param>
		/// <param name="telno">the recruit's contact number</param>
		[WebMethod] public XmlDocument Edit(string token, string name, string firstname, string surname, string email, string telno) {
			try {
				userx.Validate(token, "Recruit.Edit");
				recruitx.Update(name, firstname, surname, email, telno);
				_AddOk();
				_AddOk(recruitx.Recruit.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		*/
		/// <summary>
		/// Space: Remove a user, including all blogspaces
		/// - Duplicated from adminX.asmx for completeness of the web service
		/// </summary>
		[WebMethod]
		public XmlDocument RemoveUser(string token, string name) {
			try {
				userx.Validate(token, "User.Delete");
				userx.Get(name);
				foreach (XmlNode node in userx.User.SelectNodes("//blogspaces/blogspace")) {
					blogspacex.DeleteUser(((XmlElement)node).GetAttribute("name"), name);
				}
				userx.Delete(name);
				_AddOk(userx.User.OuterXml);
			} catch (x_exception e) { _AddError(e); } catch (System.Exception e) { _AddError(e); }
			return (Result);
		}
		#endregion

		#region Register web service methods
		/// <summary>
		/// Add a subscriber from the Recruit list. 
		///	- A recruit receives an email, confirms subscription and is added as a subscriber (here)
		/// - Optionally adds additional information
		/// </summary>
		[WebMethod]
		public XmlDocument RegisterRecruit(string token, string uid, string telno, string extrainfo1, string extrainfo2, string extrainfo3, string extrainfo4) {
			try {
				userx.Validate(token, "Recruit.Register");
				recruitx.Get(uid);
				string username = recruitx.Email;
				if (userx.Is(username))
					userx.Update(username, recruitx.FirstName, recruitx.Surname, recruitx.Email, telno, DEFAULT_GROUP, DEFAULT_PASSWORD, extrainfo1, extrainfo2, extrainfo3, extrainfo4);
				else
					userx.Add(username, recruitx.FirstName, recruitx.Surname, recruitx.Email, telno, DEFAULT_SECRET, DEFAULT_PASSWORD, DEFAULT_GROUP, extrainfo1, extrainfo2, extrainfo3, extrainfo4);
				blogspacex.AddUser(recruitx.Blogspace, username, DEFAULT_GROUP);
				userx.AddBlogspace(recruitx.Blogspace, username, DEFAULT_GROUP, true);
				_AddOk(userx.User.OuterXml);
				//Login(username, DEFAULT_PASSWORD);
			} catch (x_exception e) { _AddError(e); } catch (System.Exception e) { _AddError(e); }
			return (Result);
		}
		#endregion
	}
}
